package com.txzt.gaea.base.controller;

import com.txzt.base.constant.RtnConstant;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.base.dto.DataPermissionDTO;
import com.txzt.gaea.base.model.DataPermission;
import com.txzt.gaea.base.service.DataPermissionService;
import com.txzt.gaea.base.vo.DataPermissionVo;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.page.PageInfoBT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/data/permission")
public class DataPermissionController extends BaseController {

    @Autowired
    private DataPermissionService dataPermissionService;

    @PostMapping(value = "/list")
    public RtnInfo<PageInfoBT<DataPermissionVo>> pageList(@RequestBody DataPermissionDTO dataPermissionDTO){
        return dataPermissionService.list(dataPermissionDTO);
    }

    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo add(@RequestBody DataPermission dataPermission){
        if (null != dataPermission && null == dataPermission.getId()){
            //查重
            if (!dataPermissionService.addCheck(dataPermission)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查角色是否重复设置");
            }
            int num = dataPermissionService.saveInfo(dataPermission);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据添加失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @PostMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo edit(@RequestBody DataPermission dataPermission){
        if (null != dataPermission && null != dataPermission.getId()){
            //查重
            if (!dataPermissionService.updateCheck(dataPermission)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查角色是否重复设置");
            }
            int num = dataPermissionService.updateInfo(dataPermission);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据更新失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/item/{id}")
    public RtnInfo getInfoById(@PathVariable Long id){
        return RtnInfo.success(dataPermissionService.getInfo(id));
    }

    @GetMapping(value = "/delete/{id}")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo remove(@PathVariable Long id){
        return RtnInfo.success(dataPermissionService.deleteInfo(id));
    }
}
